/**
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
参数：
target: 对于静态成员来说是类的构造函数，对于实例成员是类的原型对象。
propertyKey: 成员的名字
*/


//给属性添加校验方法
function Min(limit: number): PropertyDecorator {
    return function (target: Object, propertyKey: string) {
        let value: string;

        const getter = function () {
            return value;
        };

        const setter = function (newVal: string) {
            delete (<any>target).errors;
            if (newVal.length < limit) {

                //throw new Error(`Your password should be bigger than ${limit}`);

                Object.defineProperty(target, 'errors', {
                    value: `Your password should be bigger than ${limit}`, configurable: true
                });
            }

            value = newVal;
        };
        Object.defineProperty(target, propertyKey, {
            get: getter,
            set: setter
        });
    }
}

class User {
    username: string;

    @Min(8)
    password: string;

    constructor(username: string, password: string) {
        this.username = username;
        this.password = password;
    }

    toString(): string {
        return `Username: ${this.username} Password: ${this.password}`;
    }
}

let danyUser = new User("dany", "pass");

//danyUser.password = "123456789";
console.log(danyUser.toString());
console.log((<any>danyUser).errors);


